import { find } from 'lodash-es';
import { parse } from 'querystring';

import { ActivityInfo, Goods, ResponseIPageActivityInfo } from '@/types/apiType';

import { apiActivityListByGet } from './activityInfo';
import { apiGoodsListByGet } from './goods';
import { apiWinMyWinByGet } from './win';

/**
 * @description undefined
 * @tags 活动详情
 * @request get:/activity/list
 */
export const apiActivityInfoByGet = async () => {
  const res = window.location.search.replace('?', '');
  const { activityId = 1 } = parse(res);
  return apiActivityListByGet({}).then((res) => {
    const curr = find(res?.records, (item: ActivityInfo) => `${item.id}` === `${activityId}`);
    return curr ? curr : (res?.records?.[0] as ActivityInfo);
  });
};

export const apiGoodsInfoByGet = async (params: {
  activityId: number;
  pageNo?: number;
  pageSize?: number;
}) => apiGoodsListByGet(params).then((res) => res?.records?.[0] as Goods);

export const apiWinListByGet = async () =>
  apiWinMyWinByGet({}).then((res) => {
    return res?.records;
  });
